import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryColumn } from "typeorm";
import dayjs from "dayjs";
import { DATE_TIME_FORMATTER } from "@chulei-ui/utils";
import type { ContractAvailability, ContractStatus, ContractType } from "@scvzerng/esi-163";

@Entity("contract")
export class Contract {
  /**
   * 合同编号
   */
  @PrimaryColumn({ type: "bigint" })
  contract_id!: number;

  // 接受合同的人
  @Column({ type: "bigint", nullable: true })
  acceptor_id?: number;
  @Column({ type: "text", nullable: true })
  acceptor_name?: string;
  // 分配合同的ID，可以是联盟，公司或角色ID
  @Column({ type: "bigint" })
  assignee_id!: number;
  // 合同的可用性
  @Column({ type: "text" })
  availability!: ContractAvailability;
  // 买断价（仅限拍卖）
  @Column({ type: "double", nullable: true })
  buyout?: number;
  // 抵押价格（仅限快递）
  @Column({ type: "double", nullable: true })
  collateral?: number;
  // 合同确认日期
  @Column({ type: "text", nullable: true })
  date_accepted?: string;
  // 合同完成日期
  @Column({ type: "text", nullable: true })
  date_completed?: string;
  // 合同的到期日期
  @Column({ type: "text" })
  date_expired!: string;
  // 合同的创立日期
  @Column({ type: "text" })
  date_issued!: string;
  // 完成合同需要的天数
  @Column({ type: "bigint" })
  days_to_complete?: number;
  // 结束地点ID（仅限快递合同）
  @Column({ type: "bigint" })
  end_location_id?: number;
  // 如果合同是以发行人的公司名义发出的，则为true
  @Column({ type: "tinyint" })
  for_corporation!: boolean;
  // 发行人的公司ID
  @Column({ type: "bigint" })
  issuer_corporation_id!: number;
  // 发行人的角色ID
  @Column({ type: "bigint" })
  issuer_id!: number;
  @Column({ type: "text", nullable: true })
  issuer_name?: string;
  // 合同价格（用于物品交换和拍卖）
  @Column({ type: "double", nullable: true })
  price?: number;
  // 合同报酬（仅限快递）
  @Column({ type: "double", nullable: true })
  reward?: number;
  // 开始地点ID（仅限快递合同）
  @Column({ type: "bigint", nullable: true })
  start_location_id?: number;
  // 合同的状态
  @Column({ type: "text" })
  status!: ContractStatus;
  // 合同的标题
  @Column({ type: "text" })
  title!: string;
  // 合同类型
  @Column({ type: "text" })
  type!: ContractType;
  // 合同中物品的体积
  @Column({ type: "double", nullable: true })
  volume?: number;
  /**
   * 合同物品清单
   */
  @OneToMany(() => ContractItem, (ci) => ci.contract)
  items?: ContractItem[];
  /**
   * 结算金额
   */
  @Column({ type: "double", nullable: true })
  settlement_price?: number;
  /**
   * 结算折扣
   */
  @Column({ type: "integer", nullable: true })
  settlement_discount?: number;
  /**
   * 结算时的估价价值
   */
  @Column({ type: "integer", nullable: true })
  settlement_estimate_price?: number;
  /**
   * 结算时的吉他收单价
   */
  @Column({ type: "integer", nullable: true })
  settlement_jita_price?: number;
  static formContract(input: any) {
    const contract = new Contract();
    Object.assign(contract, input);
    contract.date_accepted = dayjs(contract.date_accepted).format(DATE_TIME_FORMATTER);
    contract.date_completed = dayjs(contract.date_completed).format(DATE_TIME_FORMATTER);
    contract.date_expired = dayjs(contract.date_expired).format(DATE_TIME_FORMATTER);
    contract.date_issued = dayjs(contract.date_issued).format(DATE_TIME_FORMATTER);
    return contract;
  }
}
@Entity("contract_item")
export class ContractItem {
  // 物品的唯一ID
  @PrimaryColumn({ type: "bigint" })
  record_id!: number;
  // 如果合同的发起人已经提交了这个物品，那么为true；如果发起人在合同中要求这个物品，那么为false
  @Column({ type: "tinyint", nullable: true })
  is_included?: boolean;
  // 是否为单例对象
  @Column({ type: "tinyint", nullable: true })
  is_singleton?: boolean;
  // 物品堆叠的数量
  @Column({ type: "double", nullable: true })
  quantity?: number;
  // "-1"表示该物品为单例（即不可堆叠）。如果物品恰好是一个蓝图，那么"-1"代表原始蓝图，"-2"表示蓝图复制品
  @Column({ type: "double", nullable: true })
  raw_quantity?: number;
  // 物品的类型ID
  @Column({ type: "bigint", nullable: true })
  type_id?: number;
  @ManyToOne(() => Contract, (c) => c.items)
  @JoinColumn({ name: "contract_id" })
  contract!: Contract;

  static fromContractItem(input: any) {
    const contractItem = new ContractItem();
    Object.assign(contractItem, input);
    return contractItem;
  }
}
